﻿namespace BadHabit.WPFTools.ValidationRules
{
    using System;
    using System.Globalization;
    using System.Windows.Controls;

    public class IntRangeRule : System.Windows.Controls.ValidationRule
    {
        public double? Min
        {
            get;
            set;
        }

        public double? Max
        {
            get;
            set;
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            int parameter = 0;

            try
            {
                if (((string)value).Length > 0)
                {
                    parameter = Int32.Parse((string)value);
                }
            }
            catch (Exception e)
            {
                return new ValidationResult(false, "Illegal characters or " + e.Message);
            }

            if (this.Min != null && parameter < this.Min)
            {
                return new ValidationResult(false, "Please enter value greater or equal to " + this.Min);
            }

            if (this.Max != null && parameter > this.Max)
            {
                return new ValidationResult(false, "Please enter value less or equal to " + this.Max);
            }

            return new ValidationResult(true, null);
        }
    }
}
